package com.aaa.controller;

import com.aaa.config.UploadImgUtils;
import com.aaa.pojo.*;
import com.aaa.service.*;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.xml.transform.Result;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Classname HomeController
 * @Description TODO
 * @Date 2020/12/2 13:54
 * @Created by 腾飞
 */
@Controller
//@RequestMapping("/home")
public class HomeController extends BaseController {

    @Autowired
    private UserService userService;
    @Autowired
    private MenuService menuService;
    @Autowired
    private PersonalService personalService;
    @Autowired
    private MessageService messageService;
    @Autowired
    private HeadService headService;

    private static Integer uid;

    @RequestMapping("/home")
    @PreAuthorize("hasAnyAuthority('admin','normal')")
    public String Home(){
        return "home";
    }

    //图片下载
    @RequestMapping("/posts")
    @ResponseBody
    public String posts(@RequestParam("avatarPic")MultipartFile file,HttpServletRequest request){
        // 文件保存的路径
        String filePath = UploadImgUtils.approvalFile(file);
        return filePath;
    }

    //页面加载信息
    @RequestMapping("/userinfo")
    @ResponseBody
    public Map<String,Object> userinfo(HttpServletRequest request){
        HttpSession session = request.getSession();
        uid = (Integer) session.getAttribute("uid");
        Map<String,Object> map = new HashMap<String, Object>();
        UserInfo userInfo = userService.queryById(uid);
        map.put("userinfo",userInfo);
        List<Menu> menus = menuService.queryMenuAll(userService.queryById(uid).getRoles().getRid());
        map.put("menus",menus);
        Personal personal = personalService.queryPersonalById(uid);
        map.put("person",personal);
        return map;
    }

    //修改头像
    @RequestMapping("/updatePerson")
    @ResponseBody
    public void updatePsersonal(@RequestParam("url")String pname){
        personalService.updatePersonalById(pname,uid);
    }

    //添加头像
    @RequestMapping("/insertPerson")
    @ResponseBody
    public void insertPsersonal(@RequestParam("url")String pname){
        personalService.IntegerPersonal(pname,uid);
    }

    @RequestMapping(name="查询个人信息",value = "/queryPersonalDetails")
    @ResponseBody
    public ResponseEntity<Map<String,Object>> queryPersonalDetails(){
        Map<String,Object> map = new HashMap<>();
        if(userService.queryUserDetails(uid)!=null){
            map.put("Details",userService.queryUserDetails(uid));
            map.put("states","已添加");
        }else{
            map.put("Details",userService.queryById(uid));
            map.put("states","未添加");
        }
        return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
    }

    @RequestMapping(name="添加个人信息",value = "/insertPersonalDetails")
    @ResponseBody
    public ResponseEntity<Map<String,Object>> insertPersonalDetails(@RequestBody JSONObject jsonObject){
        Integer url_id = headService.queryHeadByName(jsonObject.getString("avatarUrl")).getUrl_id();
        jsonObject.put("avatarUrl",url_id);
        userService.insertUserDetails(jsonObject);
        return new ResponseEntity<Map<String,Object>>(HttpStatus.OK);
    }

    @RequestMapping(name="修改个人信息",value = "/updatePersonalDetails")
    @ResponseBody
    public ResponseEntity<Map<String,Object>> updatePersonalDetails(@RequestBody JSONObject jsonObject){
//        String update_date = StringUtils.substringBeforeLast(jsonObject.getJSONObject("Details").getString("update_time").replace("T"," "), ".");
//        jsonObject.put("update_time",update_date);
        userService.updateUserDetails(jsonObject);
        return new ResponseEntity<Map<String,Object>>(HttpStatus.OK);
    }

    @RequestMapping(name="查询我的消息",value = "/queryPersonalmessage")
    @ResponseBody
    public ResponseEntity<Map<String,Object>> queryPersonalmessage(){
        Map<String,Object> map = new HashMap<>();
        map.put("message",messageService.queryMessage(uid));
        return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
    }
    @RequestMapping(name="修改消息状态",value = "/updateReadStatus")
    @ResponseBody
    public ResponseEntity<Map<String,Object>> updateReadStatus(@RequestBody JSONObject jsonObject){
        if(messageService.updateStatus(jsonObject.getInteger("id"))){
            Map<String,Object> map = new HashMap<>();
            map.put("message",messageService.queryMessage(uid));
            return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
        }else{
            return new ResponseEntity<Map<String,Object>>(HttpStatus.BAD_REQUEST);
        }
    }

}
